/**
 * 
 */
package com.jysz.certificate.utils;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.jysz.certificate.constant.ConfigTypeConstants.ConfigTypes;
import com.jysz.certificate.constant.ConfigTypeConstants.PerformanceConfig;
import com.jysz.certificate.persistence.model.core.SystemPreferences;
import com.jysz.certificate.service.system.SystemPreferencesService;
import com.jysz.certificate.spring.SpringBeanManager;

/**
 * @author <a href="mailto:javaengine@outlook.com">Kaca Rich</a>
 *
 */
public class CertificateUtils {
	
	public final static SystemPreferences getSystemPreferences(String configTypeKey){
		String className = SystemPreferencesService.class.getSimpleName();
		
		className = className.replaceFirst(String.valueOf(className.charAt(0)), String.valueOf((className.charAt(0))).toLowerCase());
		SystemPreferencesService systemPreferencesService = SpringBeanManager.getBean(className, SystemPreferencesService.class);

		return systemPreferencesService.findConfigByConfigType(configTypeKey);
	}
	
	public final static String getStringPropertyByKeys(String configTypeKey, String key){
		String value = null;
		JSONObject jb = CertificateUtils.getSystemPreferencesByConfigTypeKey(configTypeKey);
		if(jb != null){
			value = jb.getString(key);
		}
		
		return value;
	}
	
	public final static Double getDoublePropertyByKeys(String configTypeKey, String key){
		Double value = null;
		JSONObject jb = CertificateUtils.getSystemPreferencesByConfigTypeKey(configTypeKey);
		if(jb != null){
			value = jb.getDouble(key);
		}
		
		return value;
	}
	
	public final static Integer getIntegerPropertyByKeys(String configTypeKey, String key){
		Integer value = null;
		JSONObject jb = CertificateUtils.getSystemPreferencesByConfigTypeKey(configTypeKey);
		if(jb != null){
			value = jb.getInt(key);
		}
		
		return value;
	}
	
	public final static JSONObject getSystemPreferencesByConfigTypeKey(String configTypeKey){
		
		SystemPreferences systemPreferences = CertificateUtils.getSystemPreferences(configTypeKey);
		JSONObject jb = null;
		if(systemPreferences != null ){//用于修改时数据获取
			if(StringUtils.isNotBlank(systemPreferences.getConfigValue())){
				jb = JSONObject.fromObject(systemPreferences.getConfigValue()); 
			}
		}
		return jb;
	}

	public static Integer getIntegerPropertyByKeys(ConfigTypes performanceConfig,
			PerformanceConfig userListRemineDay) {
		return CertificateUtils.getIntegerPropertyByKeys(performanceConfig.getKey(), userListRemineDay.getField());
	}
}
